<?php
/*
* mo ta: form nay dung de thay doi ngay het hạn dich vu 
* 
* 
* 
*/
$objXajax->registerFunction("frmchangeexpire_loadGrid");
$objXajax->registerFunction("frmchangeexpire_save");
$objXajax->registerFunction("frmchangeexpire_init");

function frmchangeexpire_init(){
      global $lang,$path,$user_id,$objDbSelect;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax 
     $strResult = '';
     
     if($_SESSION['current']['power_type_id']==POWER_AGENCY){         
         //$username =$_SESSION['current']['user_name'];            
        // $strResult ="<select id='frmchangeexpire_cbbagent' class='width100'> <option value ='$user_id'>$username</option> </select>";
        $objResponse->addScript("$('#frmchangeexpire_tragent').remove();"); 
     }else{
        $sql = "Select username as name,id from tbl_user where power_type_id = ".POWER_AGENCY;
        $arr = $objDbSelect->GetArray($sql);
        
        for($i=0;$i<count($arr);$i++){
            $value = $arr[$i]['id'];
            $name =  $arr[$i]['name'];
            $strResult .= "<option value ='$value'>$name</option>";
        }
        $strResult ="<select id='frmchangeexpire_cbbagent' class='width100 cbbRequired'><option value ='' > --Tất cả-- </option>".$strResult.'</select>';
     }
     $objResponse->addAssign('frmchangeexpire_tdagent', 'innerHTML', $strResult);
    
    $objResponse->addScript('frmchangeexpire_search();'); 
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax
}
    
function frmchangeexpire_loadGrid($name,$timeexpire,$agent=''){     

    global $lang,$path;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrGrid = frmchangeexpire_getGrid($name,$timeexpire,$agent);
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/'.$path.'/main_changeexpire_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('frmchangeexpire_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('frmchangeexpire_tbl');");  
    $objResponse->addScript('changeWaiting(1);');                            
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function  frmchangeexpire_save($month,$id){
    global $objDbSelect,$user_id;
    $objResponse = new xajaxResponse();
    $power_type_id = $_SESSION['current']['power_type_id'];
    $addCondition='';
    $id = addslashes($id);
    $month = addslashes($month);
    if($power_type_id==POWER_AGENCY){
        $addCondition = ' and user_created = '.$user_id;
    }
    $sql = "update tbl_service set expire_date = DATE_ADD(expire_date,INTERVAL $month MONTH) where id = $id $addCondition ";   
    if($objDbSelect->Execute($sql)){
        $sql_expire = " INSERT INTO tbl_history_expire(date_created,user_created,month_expire,service_id) 
        values(now(),$user_id,'$month','$id') ";
        $objDbSelect->Execute($sql_expire);
    } 
    $objResponse->addScript('changeWaiting(1);');  
    $objResponse->addScriptCall('frmchangeexpire_search();');  
    return $objResponse->getXML(); 
} 
  
function  frmchangeexpire_getGrid($name,$timeexpire,$agent=''){
    global $objDbUpdate,$user_id;
    $sqlSelect = "SELECT ci.`name` as city_name,dis.`name` as dis_name, t.`address`,
 t.`name`, t.`user_created`,
 t.`expire_date`, t.`phone`,t.id,
 t.`boss`, t.`district_id`,u1.username as agentname,u2.username as bossname,if(datediff(t.expire_date,now())>45,0,1) as flg_show,
 t.`city_id` FROM tbl_service as t join tbl_city as ci on t.city_id = ci.id join tbl_district as dis on t.district_id = dis.id
 join tbl_user as u1 on t.user_created = u1.id 
join tbl_user as u2 on t.id = u2.hotel_id and u2.power_type_id = 5  WHERE t.active = 1 ";


    $condition = '';
    if($_SESSION['current']['power_type_id']==POWER_AGENCY){
       $agent =  $user_id;
    }

    if($timeexpire!=''){
      $numdate = $timeexpire*30;  
      $condition.= " AND datediff(t.expire_date,now()) <=$numdate ";
    }
    
   
    if(trim($name)!=''){
        $condition.= " AND t.name LIKE '%$name%' ";
    }
    if(trim($agent)!=''){
        $condition.= " AND u1.id = '$agent' ";
    }
    $sqlSelect.=$condition;
    //echo $sqlSelect; die();
    $arr = $objDbUpdate->GetArray($sqlSelect);
 return $arr ;
}  

//function  frmchangeexpire_edit($id){
//    global $objDbSelect;
//    $objResponse = new xajaxResponse();
//    $sql         = "select *,REPLACE(REPLACE(description, '\n\r', '<br/>'),'\n','<br>') as description1 from tbl_power_type where id = $id";
//    $arr         = $objDbSelect->GetArray($sql);
//    
//    $name_vi = $arr[0]['name_vi'];
//    $name_en = $arr[0]['name_en'];
//    $flg_cus = $arr[0]['flg_cus']==1?'true':'false';       
//    $description = addslashes($arr[0]['description1']);       
//    $objResponse->addScript("$('#frmchangeexpire_txtNameVi').val('$name_vi');");
//    $objResponse->addScript("$('#frmchangeexpire_txtNameEn').val('$name_en');");
//    $objResponse->addScript("$('#frmchangeexpire_chkCus').attr('checked',$flg_cus);"); 
//    $objResponse->addAssign('frmchangeexpire_description','innerHTML',"$description"); 
//       
//    $objResponse->addScript('changeWaiting(1);');
//    
//    return $objResponse->getXML();
//} 

//function  frmchangeexpire_delete($id){
//    global $lang;
//    $objResponse = new xajaxResponse();
//    return $objResponse->getXML();
//}


# end region process data
?>
